import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import CpasTable from "../ExcelGrid";
import "./_style.less";
import AntTabs from "~/public/cpas-ui/components-ui/ant-components/AntTab";
import { Button, Space } from "antd";
import CheckDetailsModal from "../ExcelGrid/components/Validator/CheckDetails";
import CpasEditor from "../../components-bs/Editor/index";
import notify from "devextreme/ui/notify";
import { DevButton } from "~/public/cpas-ui";
import { LoadIndicator } from "devextreme-react";
import { getCurrentSysName, SystemName, MergeType, getHbTypeByZcbfid, isFirstCharEqual, transformCrossData, } from "../../tools/Tools";
import DsBase from "./DSBase";
const XMType = window.sessionStorage.electronParams
    ? JSON.parse(window.sessionStorage.electronParams)?.params?.XMType
    : "单机审计项目";
class CpasTableDsVariant extends DsBase {
    constructor(props) {
        super(props);
        console.log("props--", props);
    }
    handleSaveData = async (name, tid, data_id, action) => {
        return this.commonSaveData(name, tid, data_id, action, true);
    };
    /**
     * 调整前单元格与原数据rowData数据对比
     * @param rmap
     * @param cmap
     * @param dsRef
     * @returns // {isChange: true/false是否更新, value: number|string 调整前的数据, oldValue: 老数据, tzValue调整后的值}
     */
    diffCellValue = (rmap, cmap) => {
        const rawData = this?.props?.innerRef?.current?.rawData; //调整前初始化源数据（未经修改）
        const tzGridRef = this?.props.innerRef?.current; //调整前的公式值
        // const tzAfterGridRef = this?.vsTableRef?.current; //调整后的公式值
        // const sourceData = tzGridRef?.hot.getSourceData();
        const row = tzGridRef.gridService.getRowIndexByRmap(rmap);
        const col = tzGridRef.gridService.getColumnIndexByCmap(cmap);
        const { tzGroup, tzType } = tzGridRef.hot.getCellMeta(row, col);
        if (tzType !== "tzq") {
            return;
        }
        if (row && col) {
            const val = tzGridRef.hot.getDataAtCell(row, col);
            // const tzVal = tzAfterGridRef.hot.getDataAtCell(row, col);
            // const tzValue = tzVal ? tzVal : null;
            if (!rawData || !rawData.length) {
                return {
                    isChange: true,
                    value: val,
                    oldValue: null,
                    // tzValue,
                };
            }
            if (rawData && rawData.length) {
                const rawCol = col - tzGroup * 2;
                let isChange = false;
                const oldCell = rawData[row] && rawData[row][rawCol] ? rawData[row][rawCol] : 0;
                if (val !== oldCell) {
                    isChange = true;
                }
                return {
                    isChange,
                    value: val,
                    oldValue: oldCell,
                    // tzValue,
                };
            }
        }
        else {
            return {
                isChange: false,
                value: null,
                oldValue: null,
                // tzValue: null,
            };
        }
    };
    render() {
        const { showDismantledTable } = this.state;
        const { renderRightButton, renderLeftButton } = this.props;
        const sysName = getCurrentSysName();
        const isSyncButton = sysName !== SystemName.MERGE && XMType !== "单机审计项目";
        const mergeType = getHbTypeByZcbfid();
        return (_jsxs("div", { className: "cpas-table-ds-container", children: [_jsxs(AntTabs, { onChange: this.tabModelChanges, activeKey: this.state.tabValue, 
                    // defaultActiveKey={"1"}
                    tabPosition: "top", children: [_jsxs(AntTabs.Panel, { tab: `附注`, className: "fz-tab-content", children: [_jsxs("div", { className: "cpas-table-ds-header", children: [_jsxs(Space, { style: { marginRight: 0 }, children: [renderLeftButton && renderLeftButton(), _jsx(DevButton, { stylingMode: "outlined", className: "cpas-ds-outline-button", text: "模板设计", onClick: () => {
                                                        this.setState({
                                                            isShowDsMenuBar: !this.state.isShowDsMenuBar,
                                                        });
                                                    } }), _jsxs(DevButton, { width: 80, stylingMode: "outlined", className: "cpas-loading-button", onClick: async () => {
                                                        this.setState({
                                                            reCalculateFormulaLoading: true,
                                                        });
                                                        const { tid = "" } = this.props.notes;
                                                        const zlTid = `${tid}-zl`;
                                                        const zlTableRef = this?.zlTableRef?.current;
                                                        const zlArtVerifyFormulaList = zlTableRef?.articulationVerifyService
                                                            ?.artVerifyFormulaList;
                                                        await this.props.innerRef.current.reCalculateFormula(async (verifyData) => {
                                                            //接口返回的数据有等号，需要过滤掉。前端展示不需要
                                                            if (verifyData && verifyData.length) {
                                                                for (let i = 0; i < verifyData.length; i++) {
                                                                    let { beVerifiedFormula = "", verifyFormula = "" } = verifyData[i];
                                                                    if (isFirstCharEqual(beVerifiedFormula)) {
                                                                        beVerifiedFormula = beVerifiedFormula.slice(1);
                                                                    }
                                                                    if (isFirstCharEqual(verifyFormula)) {
                                                                        verifyFormula = verifyFormula.slice(1);
                                                                    }
                                                                    verifyData[i]["beVerifiedFormula"] =
                                                                        beVerifiedFormula;
                                                                    verifyData[i]["verifyFormula"] = verifyFormula;
                                                                }
                                                            }
                                                            const result = await zlTableRef.articulationVerifyService.generateArtVerifyFormula(verifyData, zlTid);
                                                            if (result && result.length) {
                                                                const createDataList = verifyData.filter((item) => item.dynamicType !== "MAIN" &&
                                                                    item.dynamicType !== "CHILD" &&
                                                                    !item.dynamicCondition);
                                                                const { parentFormula, childFormula, otherChildFormula, } = await zlTableRef.articulationVerifyService.formatDynamicFormula(result, zlTid);
                                                                zlTableRef.articulationVerifyService.artVerifyFormulaList =
                                                                    [
                                                                        ...createDataList,
                                                                        ...parentFormula,
                                                                        ...childFormula,
                                                                        ...otherChildFormula,
                                                                    ];
                                                            }
                                                            else if (verifyData && verifyData.length) {
                                                                zlTableRef.articulationVerifyService.artVerifyFormulaList =
                                                                    [...verifyData];
                                                            }
                                                        }, true, zlArtVerifyFormulaList);
                                                        this.setState({
                                                            reCalculateFormulaLoading: false,
                                                        });
                                                    }, children: [_jsx(LoadIndicator, { style: {
                                                                marginRight: 5,
                                                            }, width: 20, height: 20, className: "button-indicator", visible: this.state.reCalculateFormulaLoading }), _jsx("span", { className: "dx-button-text", children: "\u91CD\u7B97" })] }), isSyncButton && (_jsxs(DevButton, { width: 70, stylingMode: "outlined", className: "cpas-loading-button", onClick: async () => {
                                                        this.setState({
                                                            reSyncLoading: true,
                                                        });
                                                        if (this.props.onSynchronous) {
                                                            await this.props.onSynchronous();
                                                        }
                                                        this.setState({
                                                            reSyncLoading: false,
                                                        });
                                                    }, children: [_jsx(LoadIndicator, { style: {
                                                                marginRight: 5,
                                                            }, width: 20, height: 20, className: "button-indicator", visible: this.state.reSyncLoading }), _jsx("span", { className: "dx-button-text", children: "\u540C\u6B65" })] })), renderRightButton && renderRightButton()] }), _jsx("div", { className: "design-vert-line" })] }), _jsx(CpasTable, { type: "ds", name: "自定义披露表", formulaTimeSlice: true, tabLevel: 3, beforeJsonExport: this.beforeJsonExport, beforeJsonImport: this.beforeJsonImport, verifyMode: true, tabGroup: "tab-ds-group", ref: this.props.innerRef, afterOnArtCellClick: (a, arr, cb) => {
                                        let p = [];
                                        const resultList = transformCrossData(this.zlTableRef.current.articulationVerifyService
                                            .artParseResultList);
                                        if (cb !== null && cb[0]) {
                                            resultList.map((item) => {
                                                const currentFormula = cb[0].currentFormula;
                                                //附注整理右键校验支持交叉校验
                                                let havePro = item.beVerifiedCoordsMap;
                                                if (havePro?.hasOwnProperty(currentFormula)) {
                                                    p.push(item);
                                                }
                                            });
                                            this.setState({
                                                result: p,
                                                isBackAll: true,
                                            });
                                            this.onShowDetailsModal();
                                        }
                                        else {
                                            return notify("非校验项/已校验通过!", "error", 2000);
                                        }
                                    }, topBar: _jsxs("div", { className: "cpas-table-ds-bar-container", children: [_jsx("div", { className: "cpas-table-ds-title", children: _jsx("span", { children: this.props.title }) }), _jsx("div", { className: "cpas-table-ds-input", children: _jsx(CpasEditor, { ref: this.titleEditorRef, tableRef: this.props.innerRef, placeholder: "\u5728\u6B64\u8F93\u5165\u9644\u6CE8\u4E2D\u62AB\u9732\u5185\u5BB9", onFocusOut: () => this.onFocusOutCallback("title") }) })] }), bottomBar: _jsx(_Fragment, { children: _jsx("div", { className: "cpas-table-ds-input cpas-table-ds-bottom-input", children: _jsx(CpasEditor, { tableRef: this.props.innerRef, ref: this.contentEditorRef, placeholder: "\u82E5\u5355\u4F53\u9644\u6CE8\u4E2D\u5B58\u5728\u5BF9\u4E0A\u8FF0\u62AB\u9732\u5185\u5BB9\u7684\u6CE8\u91CA\uFF0C\u8BF7\u5728\u6B64\u8F93\u5165", onFocusOut: () => this.onFocusOutCallback("content") }) }) }), mapRowsList: [], mapColumnsList: [], isShowMenuToolBar: this.state.isShowDsMenuBar, gridData: this.props.gridData, ...this.props, numberZeroReplace: " ", afterDocumentKeyDownHook: (e) => {
                                        if (e.keyCode === 8 || e.keyCode === 46) {
                                            console.log("退格键被按下");
                                            // 获取当前选中的单元格的坐标
                                            const gridRef = this.props.innerRef?.current;
                                            const selection = gridRef.hot.selection;
                                            const coords = selection.getSelectedRange().ranges[0].from;
                                            if (coords) {
                                                const { row, col } = coords; // 获取行和列的索引
                                                const value = e.target.value;
                                                const headerJson = gridRef.headerService.headJson;
                                                const headerComputeData = gridRef.headerService.headerComputeData(headerJson);
                                                const cHeaderColumn = headerComputeData.headerColumn;
                                                const config = cHeaderColumn && cHeaderColumn[col]
                                                    ? cHeaderColumn[col]?.config
                                                    : {};
                                                // const val = gridRef.hot.getDataAtCell(row, col);
                                                // 修改单元格数据，这里我们将其设置为空字符串
                                                if (config?.type === "numeric" && value.length < 2) {
                                                    gridRef.hot.setDataAtCell(row, col, 0);
                                                }
                                            }
                                        }
                                    } })] }, "1"), _jsxs(AntTabs.Panel, { forceRender: true, tab: `整理`, className: "zl-tab-content", children: [_jsxs("div", { className: "cpas-table-ds-header", children: [_jsxs(Space, { style: { marginRight: 0 }, children: [mergeType === MergeType.TEMPLATE ? null : (_jsx(Button, { size: "small", 
                                                    // disabled={true}
                                                    onClick: () => {
                                                        console.log(this.zlTableRef);
                                                        this.setState({
                                                            result: this.zlTableRef.current?.articulationVerifyService
                                                                .artParseResultList,
                                                            resultList: this.zlTableRef.current?.articulationVerifyService
                                                                .artVerifyFormulaList,
                                                            isBackAll: false,
                                                        });
                                                        this.onShowDetailsModal();
                                                    }, children: "\u6821\u9A8C\u8BE6\u60C5" })), _jsx(Button, { onClick: () => {
                                                        this.zlTableRef.current?.menuToolRef?.current?.reductionEditorRef?.current?.setIsHiddenMergeRow(this.zlTableRef.current?.tableConfig?.filterCfg?.num
                                                            ? true
                                                            : false);
                                                        this.setState({
                                                            isShowZlMenuBar: !this.state.isShowZlMenuBar,
                                                        });
                                                    }, size: "small", children: "\u6574\u7406\u8BBE\u8BA1" })] }), _jsx("div", { className: "design-vert-line" })] }), _jsx("div", { ref: this.cpasstableWapperRef, id: "cpasstableWapperRef", children: _jsx(CpasTable, { tabLevel: 3, tabGroup: "tab-ds-group", verifyMode: true, hiddenTableContainer: true, mapRowsList: [], mapColumnsList: [], isShowMenuToolBar: this.state.isShowZlMenuBar, gridData: this.props.gridData, ref: this.zlTableRef, afterOnArtCellClick: (a, arr, cb) => {
                                            let p = [];
                                            const resultList = transformCrossData(this.zlTableRef.current.articulationVerifyService
                                                .artParseResultList);
                                            if (cb !== null && cb[0]) {
                                                resultList.map((item) => {
                                                    const currentFormula = cb[0].currentFormula;
                                                    //附注整理右键校验支持交叉校验
                                                    let havePro = item.beVerifiedCoordsMap;
                                                    if (havePro?.hasOwnProperty(currentFormula)) {
                                                        p.push(item);
                                                    }
                                                });
                                                this.setState({
                                                    result: p,
                                                    isBackAll: true,
                                                });
                                                this.onShowDetailsModal();
                                            }
                                            else {
                                                return notify("非校验项/已校验通过!", "error", 2000);
                                            }
                                        }, ...this.props, type: "zl", dsTableRef: this, numberZeroReplace: " " }) }), _jsx("div", { className: "zl-table-wapper", children: _jsx("div", { id: "cpasstableWapperRef2", style: {
                                            height: this.state.isShowZlMenuBar
                                                ? "calc(100% - 150px)"
                                                : "100%",
                                            display: showDismantledTable ? "block" : "none",
                                            overflowY: "auto",
                                        }, children: "\u8FD9\u91CC\u663E\u793A\u62C6\u8868\u540E\u7684\u6570\u636E" }) })] }, "2")] }), _jsx(CheckDetailsModal, { isOpen: this.state.isOpenDetails, handleOpen: this.onShowDetailsModal, result: this.state.result, onBackAll: this.onBackAll, isBackAll: this.state.isBackAll, resultList: this.state.resultList, params: this.zlTableRef })] }));
    }
}
export default CpasTableDsVariant;
